<template>
  <div class="login-wrapper">
    <!-- 登录盒子 -->
    <div class="box-login">
      <!-- 图片 -->
      <div class="login-img">
        <img src="@/assets/logo.png" alt="" />
      </div>
      <!-- 表单 -->
      <el-form :model="ruleForm" ref="login" :rules="rules" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="ruleForm.username"
          ></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="setLogin()">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ref, reactive, toRefs, computed, watch } from "vue";
import { useStore } from "vuex"; /** 引入vuex*/
import { useRoute, useRouter } from "vue-router"; /** 引入router*/
import api from "@/http/api.js";
import { fn } from "@/utils/routerlist.js";
export default {
  name: "MyLogin",
  setup() {
    /**
     * 路由
     */
    let router = useRouter();
    /**
     * 引入 vuex
     */
    let store = useStore();
    /**
     * 导出数据
     */
    const state = reactive({
      /**
       * 节流
       */
      on: true,
      /**
       * 登录模型
       */
      ruleForm: {
        username: "", //用户名
        password: "", //密码
      },
      /**
       * 表单验证
       */
      rules: {
        username: [
          { required: true, message: "请输入用户名" },
          {
            min: 2,
            max: 10,
            message: "长度在 2 到 10 个字符",
          },
        ],
        password: [
          { required: true, message: "请输入密码" },
          {
            min: 6,
            max: 18,
            message: "长度在 6 到 18 个字符",
          },
        ],
      },
    });

    /**
     * 登录方法
     */
    const login = ref();
    let setLogin = () => {
      if (state.on) {
        state.on = false;
        setTimeout(function () {
          state.on = true;
          login.value.validate((valid) => {
            if (valid) {
              /**同步请求 */
              api.getLogin(state.ruleForm).then((res) => {
                if (res.meta.status == 200) {
                  localStorage.setItem("store", res.data.token);
                  console.log(res, 111111);

                  loadRoute();

                  router.push("/homeLayout");
                }
              });
            }
          });
        }, 3000);
      }
    };

    let loadRoute = async () => {
      const res = await api.getMenu();
      console.log(res, "动态路由");
      if (res.meta.status === 200) {
        localStorage.setItem("menusList", JSON.stringify(res.data));
        console.log(res.data, "222");
        let newList = fn(res.data);
        console.log("递归后的列表路由", newList);
        newList.forEach((item) => {
          router.addRoute("homeLayout", item);
        });
      }
    };

    /**
     * 导出方法
     */
    return {
      ...toRefs(state),
      setLogin,
      login,
      router,
      loadRoute,
    };
  },
};
</script>

<style lang="scss" scoped>
// 全局
* {
  box-sizing: border-box;
}
.login-wrapper {
  background-color: #2b4b6b;
  height: 100%;
  .box-login {
    .login-img {
      width: 150px;
      height: 150px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      top: 0;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    padding: 10px;
    width: 450px;
    height: 300px;
    border-radius: 3px;
    padding-top: 120px;
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    margin: auto;
    background-color: #fff;
  }
}
</style>